import { DataTypes } from "sequelize";
import sequelize from "./db";
import MarkerModel from "./MarkerModel";
import UserModel from "./UserModel";

const JournalsModel = sequelize.define("journals", {
  m_id: {
    type: DataTypes.INTEGER,
    references: {
      model: MarkerModel,
      key: "m_id",
    },
    allowNull: false,
    comment: "marker_poi 的 id",
  },
  u_id: {
    type: DataTypes.INTEGER,
    references: {
      model: UserModel,
      key: "id",
    },
    primaryKey: true,
    allowNull: false,
    comment: "user id",
  },
  id: {
    type: DataTypes.INTEGER,
    primaryKey: true,
    unique: true,
    allowNull: false,
    autoIncrement: true, // 自增长
    comment: "journal 表每条记录的独立 id",
  },
  title: {
    type: DataTypes.STRING(50),
    allowNull: true,
  },
  content: {
    type: DataTypes.TEXT,
    allowNull: false,
  },
  photos: {
    type: DataTypes.TEXT,
    allowNull: true,
    defaultValue: "",
  },
  is_open: {
    type: DataTypes.BOOLEAN,
    allowNull: false,
    defaultValue: 1, // 1: 公开，0: 仅自己可见
  },
  ext_data: {
    type: DataTypes.TEXT,
    allowNull: true,
    defaultValue: "",
  },
});

JournalsModel.belongsTo(MarkerModel, {
  targetKey: "m_id",
  foreignKey: "m_id",
});

export default JournalsModel;
